def prob_predict(
        appearances=0,
        playing_time=0,
        goal=0,
        assist=0,
        passing=0,
        shot=0,
        foul=0,
        offside=0,
        interception=0,
        clearance_kick=0,
        save=0):
    '''
    传入的参数为某一区间的统计数据，某个球员的在某个赛季的数据
    所有参数均为整数
    :param appearances: 出场次数
    :param playing_time: 出场时间（秒数）
    :param goal: 进球个数
    :param assist: 助攻次数
    :param passing: 关键传球次数
    :param shot: 射门次数
    :param foul: 犯规次数
    :param offside: 越位次数
    :param interception: 抢断次数
    :param clearance_kick: 解围次数
    :param save: 扑救次数
    :return: Json 格式
    {
        'goal_keeper': p1 ,
        'defender': p2
        'midfielder': p3
        'forward ': p4,
        'name': name
    }
    goal_keeper: 代表门将
    defender: 代表后卫
    midfielder: 代表中场
    forward: 代表前锋
    name: 代表类比明星球员
    p1, p2, p3, p4 为float数值，代表概率
    name 为知名球员中文名字
    '''

    import pickle
    import json

    result = {
        'goal_keeper': 0.25,
        'defender': 0.25,
        'midfielder': 0.25,
        'forward': 0.25,
        'name': u'未知'
    }

    try:
        with open('prob_model.pkl', 'rb') as f:
            model = pickle.load(f)
    except:
        return json.dumps(result)

    base_value = [goal, assist, passing, shot, foul, offside, interception, clearance_kick, save]
    if playing_time == 0:
        value = base_value
    else:
        value = [v / playing_time * 90 * 60 for v in base_value]
    try:
        prob = model.predict_proba(value)
        group = model.predict(value)
    except:
        return json.dumps(result)

    result['goal_keeper'] = prob[0][0]
    result['defender'] = prob[0][1]
    result['midfielder'] = prob[0][2]
    result['forward'] = prob[0][3]

    try:
        with open('name_model.pkl', 'rb') as f:
            name_model = pickle.load(f)

        if group == 0:
            dist, pos = name_model['clf_mj'].kneighbors(value, n_neighbors=1)
            name = name_model['name'][name_model['mj_y'][pos[0][0]]]
        elif group == 1:
            dist, pos = name_model['clf_hw'].kneighbors(value, n_neighbors=1)
            name = name_model['name'][name_model['hw_y'][pos[0][0]]]
        elif group == 2:
            dist, pos = name_model['clf_zc'].kneighbors(value, n_neighbors=1)
            name = name_model['name'][name_model['zc_y'][pos[0][0]]]
        else:
            dist, pos = name_model['clf_qf'].kneighbors(value, n_neighbors=1)
            name = name_model['name'][name_model['qf_y'][pos[0][0]]]

        result['name'] = name
    except:
        return json.dumps(result)

    return json.dumps(result)


############################################################################################

#下面为调用例子
result = prob_predict(21, 1693, 2, 0, 12, 10, 35, 0, 31, 52, 0)
print(result)

